//
//  Transform.cpp
//  OpenGL StarShip
//
//  Created by Tariqul Islam on 6/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>

#include "headers/Transform.h"

#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"

#include <math.h>
#include <iostream>
using namespace std;

Transform::Transform(){
    reset();
    
}

Vector3d Transform::getPosition(){
    return this->position;
}

void Transform::setPosition(Vector3d pos){
    this->position = pos;
}

void Transform::reset() {
    this->position = Vector3d(0.0, 0.0, 0.0);
    this->along = Vector3d(1.0, 0.0, 0.0);
    this->up = Vector3d(0.0, 1.0, 0.0);
    this->forward = Vector3d(0.0, 0.0, 1.0);
    
   // up_rot=forward_rot=along_rot =0;
    update();
}

void Transform::update(){

    float viewMatrix[4][4];
    viewMatrix[0][0] = along.x;
    viewMatrix[0][1] = along.y;
    viewMatrix[0][2] = along.z;
    viewMatrix[0][3] = 0.0f;
    
    viewMatrix[1][0] = up.x;
    viewMatrix[1][1] = up.y;
    viewMatrix[1][2] = up.z;
    viewMatrix[1][3] = 0.0f;
    
    
    viewMatrix[2][0] = forward.x;
    viewMatrix[2][1] = forward.y;
    viewMatrix[2][2] = forward.z;
    viewMatrix[2][3] = 0.0f;
    
    
    viewMatrix[3][0] = position.x;
    viewMatrix[3][1] = position.y;
    viewMatrix[3][2] = position.z;
    viewMatrix[3][3] = 1.0f;
    
    glMatrixMode(GL_MODELVIEW);
    glMultMatrixf((GLfloat *)&viewMatrix);
//    glLoadMatrixf((GLfloat *)&viewMatrix);
    
    
    //glTranslatef(position.x, position.y, position.z);
//    glRotatef(up_rot,up.x,up.y,up.z);
//    glRotatef(along_rot, along.x, along.y, along.z);
//    glRotatef(forward_rot, forward.x, forward.y, forward.z);
    
}

void Transform::pitch(GLfloat theta) {
    forward = rotateVector(forward, along, theta);
    up = rotateVector(up, along, theta);
}


void Transform::yaw(GLfloat theta) {
    forward = rotateVector(forward, up, theta);
    along = rotateVector(along, up, theta);
}



void Transform::roll(GLfloat theta) {
    up = rotateVector(up, forward, theta);
    along = rotateVector(along, forward, theta);
}


void Transform::walk(GLfloat delta) {
    position = position + forward * delta;
}


void Transform::fly(GLfloat delta) {
    position = position + up * delta;   
}


void Transform::strafe(GLfloat delta) {
    position = position + along * delta;
}






